const app = getApp();
import { baseUrl } from "../../../api/request"
import { getStorage } from "../../../utils/localStorage";
import { getMapDistance } from "../../../utils/address"
import { selectCommentBytown, selectCommenTownNum, tsComment, selectScenicSpot, townShipDetails } from "../../../api/homePage"
Page({
    data: {
        navData: {
            level: 12,
            title: ''
        },
        mapData: {
            scale: 16, //缩放级别，取值范围为3-20
            minScale: 3, //最小缩放级别
            maxScale: 20, //最大缩放级别
            enable3D: true, //展示3D楼块
            enableZoom: true, //是否支持缩放
            enableScroll: true, //是否支持拖动
            enableBuilding: true, //是否展示建筑物
            tapFlag: true, //是否支持点击地图打开第三方地图功能
        },
        imgUrl: baseUrl,
        swiper: {
            autoplay: true, //自动切换
            dots: true, //指示点
            dotsColor: "#ffffff", //指示点颜色
            dotsActiveColor: "#4646A0", //选中指示点颜色
            circular: true, //衔接滑动
            duration: 3000, //滑动动画时长
            boxFlag: false,
        },
        latlng: {},
        user: getStorage("user"), //用户信息
        content: "",
        detailObj: {}, //详情页数
        scenicspotList: [], //景区列表数据
    },
    onLoad(options) {
        let id = options.id;
        app.globalData.detailPageType = "乡镇详情";  //详情页类型
        this.setData({
            imgUrl: baseUrl,
            latlng: getStorage("latLng"),
        });
        this.getInfo(id);
    },
    onShow() {

    },
    onReady() {

    },
    /* 获取乡镇详情 */
    getInfo(id) {
        let that = this.data;
        townShipDetails({ townShipId: id }).then(res => {
            res.data.data.imageScale = res.data.data.imageScale.split(",");
            res.data.data.latlng = res.data.data.longitude + "," + res.data.data.latitude;
            res.data.data.distance = getMapDistance(that.latlng.lat, that.latlng.lng, res.data.data.latitude, res.data.data.longitude);
            res.data.data.pccAddress =  res.data.data.pcc ? res.data.data.pcc.split(",") : []
            this.setData({
                detailObj: res.data.data
            });
            console.log(this.data.detailObj,'detailObj');
            this.getSelectCommentBytown();
            this.getScenicSpotList();
        });
    },
    /* 获取景区列表 */
    getScenicSpotList() {
        let that = this.data;
        selectScenicSpot({ townShipId: that.detailObj.id, longitude: this.data.latlng.lng, latitude: this.data.latlng.lat }).then(res => {
            let arr = res.data.data.map(item => ({
                ...item,
                villageImage: item.villageImage ? item.villageImage.split(",") : [],
                villageLabel: item.villageLabel ? item.villageLabel.split(",") : []
            }));
            this.setData({
                scenicspotList: arr
            });
        });
    },
    //景区列表点击事件
    scenicspotDetail(e) {
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url: '/pagesShouYe/pages/xaingcunDetail/index?id=' + item.villageId,
        });
    },
    //获取评论列表
    async getSelectCommentBytown() {
        selectCommentBytown({ townShipId: this.data.detailObj.id }).then(res => {
            if (res.data.code === 200) {
                this.setData({
                    commentList: res.data.data,
                });
                console.log(this.data.commentList, 'commentList');
            };
        });
    },
    //评论内容bindinput事件
    comment(e) {
        let value = e.detail.value;
        this.setData({
            content: value,
        });
    },
    //评论用户头像点击事件
    userClick(e) {
        const item = e.currentTarget.dataset.item;
        item.id = item.userId;
        if (item.userId === this.data.user.id) {
            wx.redirectTo({
                url: '/pages/index/index?index=' + 5,
            });
        } else {
            wx.navigateTo({
                url: '/pagesWoDe/pages/personalHomepage/index?item=' + encodeURIComponent(JSON.stringify(item)),
            });
        };

    },
    //提交按钮事件
    async submit() {
        let that = this.data;
        if (!that.content) {
            wx.showToast({
                icon: "none",
                title: "请先输入评论内容",
            });
        } else {
            tsComment({ userId: that.user.id, townShipId: that.detailObj.id, content: that.content }).then(res => {
                this.getSelectCommentBytown();
                this.setData({
                    content: "",
                });
            });
        }

    },

    /** 近期活动点击事件 */
    jqhdTap(){
        let obj = { townName: this.data.detailObj };
        wx.navigateTo({
            url: '/pagesShouYe/pages/search/index?item=' + encodeURIComponent(JSON.stringify(obj)),
        });
    },

    /** 旅游乡村更多按钮点击事件 */
    vallageMoreTap(){
        const _this = this.data;
        wx.navigateTo({
            url: '/pagesShouYe/pages/xiangCun/index?townShipName=' + _this.detailObj.pcc,
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})